/*
 * Copyright © 2021 http://www.hn-szzy.com/ All rights reserved.
 */
package com.xjh.basestudy.javabase.thread.synctype;

/**
 * @author： 徐建华
 * @date： 2022/1/16
 * @description：对象锁的同步代码块类型
 */
public class TestSyncMethod implements Runnable {
    static TestSyncMethod instance = new TestSyncMethod();
    static int i = 0;

    @Override
    public void run() {
        //写在run方法里面，这里的this指的就是本类
        synchronized (this) {
            for (int j = 0; j < 100000; j++) {
                i++;
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(instance);
        Thread t2 = new Thread(instance);
        t1.start();
        t2.start();
        //上面用了同步代码块 这里的i 稳稳的输出二十万
        System.out.println(i);
    }


}
